<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Website_Report extends CI_Controller {
	public function index(){
		if($this->session->userdata('website_id') == '' || $this->session->userdata('website_user') == ''){
			$this->load->helper(array('url'));
			redirect('/website/login', 'location', 301);
		}else{
			#$this->session->userdata('website_id').'|'.$this->session->userdata('website_user');
			$this->layout->setLayout('default');
			$this->layout->setTitle('Website | Report');
			$web = $this->session->userdata('sms_code');
			if(isset($web)){			
				$pos = strpos($web,' ');
				if ($pos === false) {			
					$filter = " AND sms_code = '$web'";
				}else{
					$syn = substr($web, 0, $pos);
					$code = substr($web, $pos + 1, strlen($web));
					$filter = " AND sms_syntax = '$syn' AND sms_code = '$code'";
				}
			}
		
			$wid = intval($this->uri->segment(1));
			$getmonth = isset($_GET['month']) ? quotes_to_entities($_GET['month']) : date('m');
			
			$month = isset($_GET['month']) ? date('Y') ."-" . quotes_to_entities($_GET['month']) : date('Y-m');
			$year = isset($_GET['year']) ? $_GET['year'] : date('Y');
			if ($year == "") $year = date('Y');
			$month = isset($_GET['month']) ? $year . '-'.str_pad(quotes_to_entities($_GET['month']), 2, "0", STR_PAD_LEFT) : date('Y-m');
				
			$filter .= " AND receive_date BETWEEN '" . $month."-01 00:00:00" . "' AND '" . $month."-31 23:59:59" . "'";
			$filmonth =  " AND T.message_request = '$web' AND T.receive_date BETWEEN '" . $month."-01 00:00:00" . "' AND '" . $month."-31 23:59:59" . "'";
			$arrDate = explode("-", $month);
			$data['month'] = $arrDate[1].'-'.$arrDate[0];
			// get earning data
			$field = "Count(T.phone)*4000 As subtotal";
			if ($web == "GN mkh198542782") $field = "Count(T.phone)*4200 As subtotal";
			$union = "";
			if ((strpos($web,"GQ APA") !== false) || (strpos($web,"GQ API") !== false)) { 
				$field = "Count(T.phone)*4800 As subtotal";
				$filmonth =  " AND (T.message_request like '$web%' or T.message_request = '$web') AND T.service_num = '8700' AND T.receive_date BETWEEN '" . $month."-01 00:00:00" . "' AND '" . $month."-31 23:59:59" . "'";
				$union = "";
				$filcode =   " AND T.message_request like '$web%' ";
			}
			if ($web == "GN mkh198542776") 
			{
				if ($year == "2012"){
					$field = "Round(Count(T.phone)*0.7,0)*4000 As subtotal";
				}else{
					$field = "Round(Count(T.phone)*0.5,0)*4000 As subtotal";
				}
				$filmonth1 =  " AND T.message_request = 'GN mkh198542776apk' AND T.receive_date BETWEEN '" . $month."-01 00:00:00" . "' AND '" . $month."-31 23:59:59" . "'";
				if ($year == "2012"){
					$field2 = "Round(Count(T.phone)*0.3,0)*4000 As subtotal";
				}else{
					$field2 = "Round(Count(T.phone)*0.5,0)*4000 As subtotal";
				}
				$union = " UNION SELECT DATE_FORMAT(T.receive_date, '%d-%m-%Y') AS earn_date, $field2 
FROM `tdx_sms_mo` T
WHERE  1=1 $filmonth1
Group by earn_date, telco, service_num, syntax, message_request";

				if ($year != "2012") $union = "";
			}
			if ($web == "GN mkh198542780") 
			{
				$field = "Count(T.phone)*4000 As subtotal";
				/*$filmonth1 =  " AND T.message_request = 'GN mkh198542780apk' AND T.receive_date BETWEEN '" . $month."-01 00:00:00" . "' AND '" . $month."-31 23:59:59" . "'";
				$union = " UNION SELECT DATE_FORMAT(T.receive_date, '%d-%m-%Y') AS earn_date,  Count(T.phone)*4000 As subtotal
FROM `tdx_sms_mo` T
WHERE  1=1 $filmonth1
Group by earn_date, telco, service_num, syntax, message_request";*/
				
				$filmonth =  " AND (T.message_request = '$web' or T.message_request = 'GN mkh198542780apk') AND T.receive_date BETWEEN '" . $month."-01 00:00:00" . "' AND '" . $month."-31 23:59:59" . "'";
				$union = "";
			}
			$sqlThisMonthEarning = "SELECT sms_ftp AS subtotal, 
									   DATE_FORMAT(receive_date, '%d-%m-%Y') AS earn_date
									FROM tdx_sms_stat WHERE 1=1 $filter";
			$sqlwebsite = "SELECT DATE_FORMAT(T.receive_date, '%d-%m-%Y') AS earn_date, $field
FROM `tdx_sms_mo` T
WHERE  1=1 $filmonth
Group by earn_date, telco, service_num, syntax, message_request"; 
			$sqlwebsite = $sqlwebsite . $union;
			$sqlThisMonthEarning = $sqlwebsite;
			$monthQuery = $this->db->query($sqlThisMonthEarning);
			if ($monthQuery->num_rows() > 0){
				$arrData = array();
				$total = 0;
				$intMonth = substr($monthQuery->row(0)->earn_date,0,2);
				$month2 = $getmonth;
				if (($getmonth < 10) && strlen($getmonth) == 1 ) $month2 = '0' . $getmonth;
				$Y = substr($monthQuery->row(0)->earn_date,6,4);
				$data['month'] = $month2 .'-'.$Y; 
				if ($intMonth > 1){					
					for ($i=1;$i<=$intMonth - 1;$i++):					
						if ($i < 10)
							$arrData['0'. $i . '-' . $month2 . '-' . $Y] = 0;
						else
							$arrData[$i . '-' . $month2 .'-'. $Y] = 0;
					endfor;
					$dd = $i . '-' . $month2 .'-'. $Y;
				}
				foreach ($monthQuery->result_array() as $row){					
					$arrData[$row['earn_date']] += intval($row['subtotal']);
					$total += intval($row['subtotal']);
				}
			}
			if (!isset($_GET['month'])){
				$strDate = 'Hôm nay ('.date('d-m-Y').')';// .$pos.'--'.$sqlThisMonthEarning;
				$data['earn_today'] = intval($arrData[date('d-m-Y')]);
			}
			$data['earn_yesterday'] = intval($arrData[date('d-m-Y', strtotime('-1 day'))]);
			$data['earn_this_month'] = $total;
			$data['str_date'] = $strDate;
			$data['month_data'] = $arrData;
			
			
			
			$data['currentUser'] = $this->session->userdata('website_user');
			$this->layout->view(THEME_DIR.'/website_report_index', $data);
		}
	}
}